package com.ppc.service.impl;

import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ppc.constant.MessageConstant;
import com.ppc.context.BaseContext;
import com.ppc.dto.EmployerClaimPageQueryDTO;
import com.ppc.entity.Claim;
import com.ppc.exception.DeletionNotAllowedException;
import com.ppc.exception.SensitiveException;
import com.ppc.mapper.employer.EmployerClaimMapper;
import com.ppc.mapper.employer.EmployerMapper;
import com.ppc.result.PageResult;
import com.ppc.service.EmployerClaimService;
import com.ppc.vo.EmployerClaimDetailVO;
import com.ppc.vo.EmployerClaimMessageVO;
import com.ppc.vo.EmployerClaimQueryVO;
import java.time.LocalDateTime;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 雇主申诉实现
 *
 * @author LENOVO
 * @date 2023/12/20
 */
@Service
public class EmployerClaimServiceImpl implements EmployerClaimService {


  @Autowired
  EmployerClaimMapper employerClaimMapper;


  @Autowired
  private EmployerMapper employerMapper;


  /**
   * 雇主分页查询申诉订单
   *
   * @param
   * @return
   */
  @Override
  public PageResult claimPageQuery(EmployerClaimPageQueryDTO employerClaimPageQueryDTO) {
    PageHelper.startPage(employerClaimPageQueryDTO.getPage(),
        employerClaimPageQueryDTO.getPageSize());

    Page<EmployerClaimQueryVO> page = employerClaimMapper.claimPageQuery(employerClaimPageQueryDTO);
    long total = page.getTotal();
    List<EmployerClaimQueryVO> records = page.getResult();

    return new PageResult(total, records);
  }

  /**
   * 创建申诉
   *
   * @param taskId
   * @param claimType
   * @param claimReason
   */
  @Override
  public void createClaim(Long taskId, Integer claimType, String claimReason) {
    Long userId = BaseContext.getCurrentId();
    Long employerId = employerMapper.getEmployerIdByUserId(userId);
    if(claimType!=1&&claimType!=2&&claimType!=3) {
      throw new DeletionNotAllowedException("请选择正确的申诉类型");
    }
    Long checkEmployerId = employerClaimMapper.getEmployerIdByTaskId(taskId);
    if (employerId != checkEmployerId) {
      throw new DeletionNotAllowedException("该订单您无法申诉");
    } else {
      Long notifyId = employerClaimMapper.queryNotifyId(taskId);
      Claim claim = Claim.builder().
              taskId(taskId).
              claimerId(BaseContext.getCurrentId()).
              claimerType(0).
              claimType(claimType).
              claimReason(claimReason).
              claimStartTime(LocalDateTime.now()).
              claimStatus(0).
              claimResult(2).
              notifyId(notifyId).
              build();
      //调用mapper层进行插入claim
      employerClaimMapper.insert(claim);
    }
  }

  /**
   * 查看申诉详细信息
   *
   * @param claimId
   * @return {@link EmployerClaimDetailVO}
   */
  @Override
  public EmployerClaimDetailVO checkDetail(Long claimId) {
    EmployerClaimDetailVO employerClaimDetailVO = employerClaimMapper.check(claimId);
    return employerClaimDetailVO;
  }

  /**
   * 雇主查看申诉消息
   *
   * @param page
   * @param pageSize
   * @return
   */
  @Override
  public PageResult checkClaimMessage(Integer page, Integer pageSize) {
    Long userId = BaseContext.getCurrentId();
    Long employerId = employerMapper.getEmployerIdByUserId(userId);
    //进行分页查询
    PageHelper.startPage(page, pageSize);
    Page<EmployerClaimMessageVO> p = employerClaimMapper.getClaimMessage(employerId, userId);
    return new PageResult(p.getTotal(), p.getResult());
  }

}
